package com.learn.springboot.service.utils;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.LFUCache;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;

/**
 * @author suzheng
 * @date 2024-05-16
 **/
public class TokenUtil {

    public static final String ADMIN_TOKEN = "admin-token";
    private static final LFUCache<Object, Object> tokenCache = CacheUtil.newLFUCache(100);

    public static String generateToken(String userName) {
        String token = UUID.fastUUID().toString(true);
        tokenCache.put(token, userName);
        return token;
    }

    public static void delToken(String token) {
        if (StrUtil.isNotBlank(token)) {
            tokenCache.remove(token);
        }
    }

    public static boolean checkToken(String token) {
        return tokenCache.containsKey(token);
    }
}
